/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unibo.as.server;

import it.unibo.as.SessionBeans.TablesGameBean;
import it.unibo.as.client.Table;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import it.unibo.as.HeartsLogic.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.servlet.http.HttpSession;
import net.zschech.gwt.comet.server.CometSession;


import it.unibo.as.client.GWTGameService;
import it.unibo.as.client.Message;
import java.util.Arrays;
import java.util.Iterator;
import java.util.UUID;

/**
 *
 * @author Johnny
 */
public class GWTGameServiceImpl extends RemoteServiceServlet implements GWTGameService {
    @EJB
    private TablesGameBean tablesGameBean;

    private static ArrayList<Table> CONTACTS = new ArrayList<Table>(){{
        add(new Table("John", 0, 0));
        add(new Table("Joe1", 0, 1));
        add(new Table("Joe2", 1, 1));
        add(new Table("Joe3", 0, 2));
        add(new Table("Joe4", 1, 3));
        add(new Table("Joe5", 1, 1));
        add(new Table("Joe6", 2, 1));
        add(new Table("Joe7", 3, 1));
    }};


    @Override
    public ArrayList<Table> fetchTablesPage(int start, int length){

        int end = length >= tablesGameBean.getTableList().size() ? tablesGameBean.getTableList().size() : length;
        //List<Table> sub = (List<Table>) CONTACTS.subList(start, end);
        //ArrayList<Table> sub = 
        ArrayList<Table> sub = new ArrayList<Table>((List<Table>) tablesGameBean.getTableList().subList(start, end));


        return sub;
    }


    @Override
    public Boolean playCard(it.unibo.as.client.Card card) {
        String debugMsg;
        ArrayList<Player> playersInTable;

        HttpSession httpSession = getThreadLocalRequest().getSession(false);
	if (httpSession == null) {
            return Boolean.FALSE;
		//TODO throw new ChatException("not logged in: no http session");
	}

	// get the user name for the HTTP session.
	String username = (String) httpSession.getAttribute("user");
	if (username == null) {
            return Boolean.FALSE;
		//TODO throw new ChatException("not logged in: no http session username");
	}

	// create the chat message
	Message message = new Message();
	message.setUsername(username);

        debugMsg = "I PLAY " + card.toString();

	message.setMessage(debugMsg);

        // questo manda a tutti. inviare solo ai giocatori nel tavolo
        /*
	for (Map.Entry<String, CometSession> entry : GWTAccountServiceImpl.users.entrySet()) {
		entry.getValue().enqueue(message);
                entry.getKey()
	}
        //
        */


        // gets all players in table send state to those
        playersInTable = new ArrayList<Player>(Arrays.asList(tablesGameBean.getTableGameFromPlayer(username).getLogic().getPlayerArray()));
        Iterator i = playersInTable.iterator();
        Player currPlayer;
        while(i.hasNext()){
            currPlayer = (Player)i.next();
            GWTAccountServiceImpl.users.get(currPlayer.getPlayerID()).enqueue(message);
            
        }
        
        
        
        
        return Boolean.TRUE;
    }

    @Override
    public Boolean createTable(String tableID, Boolean bfourDeals, int nAIPlayers){
        HttpSession httpSession = getThreadLocalRequest().getSession(false);
        String authorID = (String) httpSession.getAttribute("user");
        int nActivePlayers = 1;

        if(tableID == null || tablesGameBean.getTableGameFromID(tableID)!= null){
            tableID = UUID.randomUUID().toString();
        }

        return tablesGameBean.createTableGame(authorID, tableID, nAIPlayers, nActivePlayers);

    }

}
